/** 
 * Copyright (C), 2010-2012, TIETIAO Software Co.,Ltd. ALL RIGHTS RESERVED. 
 * File name:BrowesAction.java
 * Version:1.0
 * Date:2011-12-18
 * Description: 浏览ACTION
 * author:zhijian.zhang
 */
package com.tietiao.action.commodity;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.tietiao.common.action.BaseAction;
import com.tietiao.common.util.PropertyUtil;
import com.tietiao.dao.model.CommodityInfo;
import com.tietiao.dao.model.SearchLableStatistics;
import com.tietiao.service.commodity.CommodityService;
import com.tietiao.service.search.SearchService;

@Controller
@Scope("prototype")
@SuppressWarnings("serial")
public class BrowseAction extends BaseAction {
	@Resource
	private CommodityService commodityService;
	@Resource
	private SearchService searchService;
	// 商品列表信息
	private List<CommodityInfo> commodityInfos;
	// 热门标签列表信息
	private List<SearchLableStatistics> searchLableStatistics;
	// 排序方式Code
	private int orderType = 0;
	// 标签名称
	private String lableName;
	// 图片地址访问路径
	private String rootPicPath = PropertyUtil.getValue("PIC_SERVER");
	// 其它标签需要传递的值
	private String otherLables;

	// 列表数量
	private Integer commdityLength = 0;
	// 页号
	private Integer pageNo;
	// 页大小
	private Integer pageSize;

	public Integer getCommdityLength() {
		return commdityLength;
	}

	public void setCommdityLength(Integer commdityLength) {
		this.commdityLength = commdityLength;
	}

	public Integer getPageNo() {
		return pageNo;
	}

	public void setPageNo(Integer pageNo) {
		this.pageNo = pageNo;
	}

	public Integer getPageSize() {
		return pageSize;
	}

	public void setPageSize(Integer pageSize) {
		this.pageSize = pageSize;
	}

	/**
	 * @return the otherLables
	 */
	public String getOtherLables() {
		return otherLables;
	}

	/**
	 * @param otherLables
	 *            the otherLables to set
	 */
	public void setOtherLables(String otherLables) {
		this.otherLables = otherLables;
	}

	/**
	 * @return the rootPicPath
	 */
	public String getRootPicPath() {
		return rootPicPath;
	}

	/**
	 * @param rootPicPath
	 *            the rootPicPath to set
	 */
	public void setRootPicPath(String rootPicPath) {
		this.rootPicPath = rootPicPath;
	}

	/**
	 * @return the commodityInfos
	 */
	public List<CommodityInfo> getCommodityInfos() {
		return commodityInfos;
	}

	/**
	 * @param commodityInfos
	 *            the commodityInfos to set
	 */
	public void setCommodityInfos(List<CommodityInfo> commodityInfos) {
		this.commodityInfos = commodityInfos;
	}

	/**
	 * @return the searchLableStatistics
	 */
	public List<SearchLableStatistics> getSearchLableStatistics() {
		return searchLableStatistics;
	}

	/**
	 * @param searchLableStatistics
	 *            the searchLableStatistics to set
	 */
	public void setSearchLableStatistics(
			List<SearchLableStatistics> searchLableStatistics) {
		this.searchLableStatistics = searchLableStatistics;
	}

	/**
	 * @return the orderType
	 */
	public int getOrderType() {
		return orderType;
	}

	/**
	 * @param orderType
	 *            the orderType to set
	 */
	public void setOrderType(int orderType) {
		this.orderType = orderType;
	}

	/**
	 * @return the lableName
	 */
	public String getLableName() {
		return lableName;
	}

	/**
	 * @param lableName
	 *            the lableName to set
	 */
	public void setLableName(String lableName) {
		this.lableName = lableName;
	}

	/**
	 * 
	 * 功能：进入浏览主页
	 * 
	 * @author: zhijian.zhang
	 * @Date: 2011-12-20
	 * 
	 * @returnsearchCommodityInfos
	 */
	public String browse() {
		initSearchLableStatistics();
		StringBuilder str = new StringBuilder();
		for (int i = 0, len = searchLableStatistics.size(); i < len; i++) {
			str.append(searchLableStatistics.get(i).getSearchLabel());
			if (i < len - 1) {
				str.append(",");
			}
		}
		otherLables = str.toString();
		return DEFAULT;
	}

	/**
	 * 
	 * 功能：异步加载商品数据
	 * 
	 * @author: zhijian.zhang
	 * @Date: 2011-12-20
	 * 
	 * @return
	 * @throws Exception
	 */
	public String loadCommodityInfos() throws Exception {

		Integer userId = null;
		if (getUser() != null) {
			userId = getUser().getUserId();
		}
		commodityInfos = commodityService.searchCommodityInfos(lableName, null,
				null, userId, (pageNo - 1) * pageSize, pageSize,
				convertOrderType());
		commdityLength = commodityInfos.size();
		return DEFAULT;
	}

	/**
	 * 功能：初始热门标签值
	 * 
	 * @author: zhijian.zhang
	 * @Date: 2011-12-20
	 * 
	 */
	private void initSearchLableStatistics() {
		searchLableStatistics = searchService.getSearchLableStatistics();
	}

	/**
	 * 功能：转换排序方式
	 * 
	 * @author: zhijian.zhang
	 * @Date: 2011-12-20
	 * 
	 * @return
	 */
	private String convertOrderType() {

		switch (orderType) {
		case 0:
			return "TIETIAO_NUM DESC";
		case 1:
			return "TIETIAO_NUM ASC";
		case 2:
			return "UPLOADER_TIME DESC";
		case 3:
			return "UPLOADER_TIME ASC";
		default:
			return "TIETIAO_NUM DESC";
		}
	}
}
